#include "nnec/nnec_utils.h"

using namespace std;

namespace NNEC
{
    vector<double> vector_add(const vector<double> & a, const vector<double> &b)
    {
        if(a.size() != 3 || b.size() !=3) {
            return vector<double>(0);
        }
        else {
            vector<double> tmp(3);
            tmp[0] = a[0] + b[0];
            tmp[1] = a[1] + b[1];
            tmp[2] = a[2] + b[2];
            return tmp;
        }
    }

    vector<double> vector_sub(const vector<double> & a, const vector<double> &b)
    {
        if(a.size() != 3 || b.size() !=3) {
            return vector<double>(0);
        }
        else {
            vector<double> tmp(3);
            tmp[0] = a[0] - b[0];
            tmp[1] = a[1] - b[1];
            tmp[2] = a[2] - b[2];
            return tmp;
        }
    }

    vector<double> vector_scale_mul(double a, const vector<double> & v)
    {
        if(v.size() != 3) {
            return vector<double>(0);
        }
        else {
            vector<double> tmp(3);
            tmp[0] = a * v[0];
            tmp[1] = a * v[1];
            tmp[2] = a * v[2];
            return tmp;
        }
    }

    double vector_dot_mul(const vector<double> & a, const vector<double> & b)
    {
        if(a.size() != 3 || b.size() !=3) {
            return 0.0;
        }
        else {
            return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
        }
    }

    vector<double> vector_cross_mul(const vector<double> & a, const vector<double> & b)
    {
        if(a.size() != 3 || b.size() !=3) {
            return vector<double>(0);
        }
        else {
            vector<double> tmp(3);
            tmp[0] = a[1] * b[2] - a[2] * b[1];
            tmp[1] = a[2] * b[0] - a[0] * b[2];
            tmp[2] = a[0] * b[1] - a[1] * b[0];
            return tmp;
        }
    }

    double vector_norm(const vector<double> & a)
    {
        if(a.size() != 3) {
            return (double)0.0;
        }
        else {
            return sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
        }
    }

    vector<double> vector_normalize(const vector<double> &a)
    {
        if(a.size() != 3) {
            return vector<double>(0);
        }
        else {
            double length = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
            if(length < 1.0e-16) {
                return vector<double>(0);
            }
            else {
                vector<double> tmp(3);
                tmp[0] = a[0] / length;
                tmp[1] = a[1] / length;
                tmp[2] = a[2] / length;
                return tmp;
            }
        }
    }
}
